package com.xw.Task;

import javax.annotation.Resource;
import javax.mail.Message;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.xw.Mail.Parser.MessageParser;
import com.xw.Service.MailService;

/**
 * 邮件解析任务 <br>
 * 每间隔十秒处理一次新邮件，上一次处理完到下一次处理完中间间隔十秒
 * @author YangJianquan
 */
@Component
public class ParserMailTask {
	private static Logger logger = LoggerFactory.getLogger(GetMailTask.class);  
	@Autowired
	private MailService mailService;
	@Resource
	private MessageParser messageParser;
	
	/**
	 * 邮件队列读取线程类，从待解析的队列中读取邮件并解析
	 */
	@Scheduled(fixedRate=200)
	@Async
	public void parseNewMails(){
		try {
			Message message = mailService.pollNewMailQueue();
			if(message != null){
				logger.info("开始解析新邮件<"+message.getSubject()+">");
				messageParser.parse(message);
			}
		} catch (Exception e) {
			logger.error("解析邮件发生异常",e);
		}
	}
}
